In real-world data analysis, your data will likely:
Fortunately, pandas can help you with all of this!
cols_to_keep = ['INSTNM', 'STABBR', 'GRAD_DEBT_MDN_SUPP']
debt_df = full_df[cols_to_keep]
debt_df.columns = ['name', 'state', 'debt']
debt_df.head().loc[] method (note the brackets)& (and) and | (or).isin() method: checks to see if value is in list of valuestx_debt_df = debt_df[(debt_df['debt'] != 'PrivacySuppressed') & (debt_df['state'] == 'TX')]
# Alternatively, use the .query() method
# tx_debt_df = debt_df.query('debt != "PrivacySuppressed" & state == "TX" ')states = ['OK', 'NM', 'TX', 'LA']
sw_debt_df = debt_df[(debt_df['debt'] != 'PrivacySuppressed') & (debt_df['state'].isin(states))]
sw_debt_df.head().assign() method# Must use index-based labeling in this new column definition
df1['col4'] = df1['col1'] + df1['col2']
# With .assign()
df2 = df1.assign(col5 = df1.col3 / df1.col4)
df2.head()dtype conversion.astype() methodSettingWithCopyWarning.dropna() method: delete all rows (or columns) that have any missing values (NaN in pandas).fillna() method: fill in missing data with a specified valuepandas: .groupby() method!Process:
.groupby() in pandasseabornimport seaborn as sns
sns.set(style = "darkgrid")
sns.boxplot(x = 'state', y = 'debtnum', data = sw_debt_df2)seaborngrid = sns.FacetGrid(data = sw_debt_df2, col = 'state', col_wrap = 2)
grid.map(sns.kdeplot, 'debtnum').merge() method in pandaspandashow parameter): 'inner' (default), 'left', 'right', and 'outer'from pandas_datareader import wb
countries = ['ZA', 'BR', 'US']
tfr = wb.download(indicator = 'SP.DYN.TFRT.IN',
country = countries, start = 1960,
end = 2018).reset_index()
tfr.head().pivot() method in pandastfr_wide = tfr.pivot(index = 'year', columns = 'country',
values = 'SP.DYN.TFRT.IN')
tfr_wide.head()pd.melt() function in pandastfr_long = pd.melt(tfr_wide.reset_index(), id_vars = 'year',
var_name = 'country', value_name = 'tfr')
tfr_long.head()urban_long['year'] = urban_long['year'].astype(int)
sns.lineplot(x = "year", y = "tfr",
hue = "country", data = urban_long)